/*
 * RedisUtil.java
 * Copyright (c) 2011,融众网络技术有限公司(www.11186.com)
 * All rights reserved.
 * ---------------------------------------------------------------------
 * 2011-11-22 Created
 */
package com.rongzhong.time.util;

import java.util.HashSet;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import com.rongzhong.core.common.util.DateUtils;

/**
 * 
 * @author wufei
 * @version 1.0 2011-11-22
 * @since 1.0
 * @see com.rongzhong.time.util.RedisUtil.java
 */
public class RedisUtil {
	private static JedisPool pool;
	
	static {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(100);
		config.setMaxIdle(20);
		config.setMaxWait(1000);
		config.setTestOnBorrow(true);
		pool = new JedisPool(config, "192.168.1.48");
	}
	
	public void set(String key, String value) {
		Jedis jedis = pool.getResource();
		try {
			jedis.zadd(key, DateUtils.now().getTime(), value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			pool.returnResource(jedis);
		}
	}

	public String get(String key) {
		String result = null;
		Jedis jedis = pool.getResource();
		try {
			result = jedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			pool.returnResource(jedis);
		}
		return result;
	}

	public Set<String> list(String key) {
		Jedis jedis = pool.getResource();
		Set<String> set = null;
		try {
			set = jedis.zrange(key, 0, 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			pool.returnResource(jedis);
		}
		if (set == null)
			return new HashSet<String>();
		return set;
	}

}
